package com.uha.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uha.reggie.common.CustomException;
import com.uha.reggie.entity.Category;
import com.uha.reggie.entity.Dish;
import com.uha.reggie.entity.Setmeal;
import com.uha.reggie.mapper.CategoryMapper;
import com.uha.reggie.service.CategoryService;
import com.uha.reggie.service.DishService;
import com.uha.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author uha
 * @date 2023/6/15
 * @apiNote
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类，删除之前需要判断
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();

        dishQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishQueryWrapper);

        setmealQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealQueryWrapper);

        //是否关联相应的菜品和套餐  若关联 抛出业务异常
        if (count1 > 0){
            throw new CustomException("当前分类下关联了菜品，不能删除~");
        }

        if (count2 > 0){
            throw new CustomException("当前分类下关联了套餐，不能删除~");

        }

        //正常删除
        super.removeById(id);

    }






}
